package day12;

import cn.hutool.core.io.IoUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import static day12.homework02.insertnames;

public class homework05 {
    public static void main(String[] args) throws Exception {
        BufferedReader reader1 = IoUtil.getReader(new FileReader("womannames.txt"));
        BufferedReader reader2 = IoUtil.getReader(new FileReader("mannames.txt"));
        ArrayList<String> names = insertnames(reader1, reader2);
        reader2.close();
        reader1.close();
        ArrayList<String> namesten = namesten(names);
//        int sum=0;
//        namedbefore(namesten);
//        numcheck(sum);
//        namedafter(sum);
        named(namesten);
    }

    private static void named(ArrayList<String> namesten) throws IOException {
        ArrayList<String> named = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("work05named02.txt"));

        String line;
        int num=0;
        String name;
        StringBuffer sb=new StringBuffer();
        while ((line = br.readLine()) != null) {
            named.add(line);
            sb.append(line+",");
        }
        br.close();
        for (int i = 0; i < sb.length(); i++) {
            char c=sb.charAt(i);
            if(c=='d'){
                num++;
            }
        }

        while(true) {
            if(num==10){
                sb=new StringBuffer(sb.toString().replaceAll("d",""));
            }
            Collections.shuffle(namesten);
             name = namesten.remove(0);
             int index=sb.indexOf(name);
             if(index<=0){
                 break;
             }
            if(sb.substring(index,index+name.length()+2).contains("d")){
                continue;
            }
            System.out.println(name);
            break;
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter("work05named02.txt",false));
        sb=new StringBuffer(sb.toString().replace(name, name.concat("d")));
        String[] str=sb.toString().split(",");
        for (int i=0;i<str.length;i++){
            bw.write(str[i]);
            bw.newLine();
        }
        bw.close();
    }

//    private static void numcheck(int sum) throws IOException {
//        BufferedReader br1=new BufferedReader(new FileReader("work05num.txt"));
//        int a;
//        while((a= br1.read())!=-1){
//            if(sum>=9){
//                sum=0;
//                BufferedWriter bw = new BufferedWriter(new FileWriter("work05named.txt",false));
//                BufferedWriter bw1 = new BufferedWriter(new FileWriter("work05num.txt",false));
//                bw.write(" ");
//                bw1.write(" ");
//                bw1.close();
//                bw.close();
//            }
//            sum+=a;
//        }
//        System.out.println(sum);
//        br1.close();
//    }
//
//
//    private static void namedafter(int sum) throws IOException {
//        BufferedReader br=new BufferedReader(new FileReader("work05nameRondom.txt"));
//        BufferedWriter bw = new BufferedWriter(new FileWriter("work05named.txt",true));
//        BufferedReader br1=new BufferedReader(new FileReader("work05named.txt"));
//        String line;
//        ArrayList<String> nametenrm=new ArrayList<>();
//        ArrayList<String> named=new ArrayList<>();
//        while((line=br.readLine())!=null){
//            nametenrm.add(line);
//        }
//        while((line=br1.readLine())!=null){
//            named.add(line);
//        }
//        RM:while(true){
//            Random rm=new Random();
//            int index=rm.nextInt(nametenrm.size());
//            String name=nametenrm.get(index);
//            fi:for (int i=0;i<named.size();i++){
//                if(name==named.get(i)){
//                    continue RM;
//                }
//            }
//            System.out.println(name);
//            bw.write(name);
//            bw.newLine();
//            break RM;
//        }
//        br.close();
//        bw.close();
//    }
//
//    private static void namedbefore(ArrayList<String> namesten) throws IOException {
//        BufferedWriter bw = new BufferedWriter(new FileWriter("work05nameRondom.txt"));
//        BufferedWriter bw1 = new BufferedWriter(new FileWriter("work05num.txt",true));
//        BufferedWriter bw2 = new BufferedWriter(new FileWriter("work05named.txt",true));
//        int j=1;
//        Random rm=new Random();
//        for (int i=0;i<10;i++) {
//            int index=rm.nextInt(namesten.size());
//            String name = namesten.remove(index);
//            bw.write(name);
//            bw.newLine();
//        }
//        bw1.write(j);
//        bw.close();
//        bw1.close();
//        bw2.close();
//    }


    public static ArrayList<String> namesten(ArrayList<String> names) throws IOException {
        ArrayList<String> namesten = new ArrayList<>();
//        BufferedWriter writer = new BufferedWriter(new FileWriter("work05named02.txt"));
        Random rm = new Random(1245L);
        for (int i = 0; i < 10; i++) {
            int number = rm.nextInt(names.size());
            String name=names.get(number);
            namesten.add(name);
//            writer.write(name);
//            writer.newLine();
        }
//        writer.close();
        return namesten;
    }
}
